<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px">
      <el-form-item label="客户" prop="customerId">
        <matic-select v-model:value="queryParams.customerId" :url="`customer/getAllList/forManager`"
                   @change="queryParams.companyId=''"   :objMap="{ value: 'id', label: 'title' }" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="企业" prop="companyId">
      <el-form-item>
        <matic-select v-model:value="queryParams.companyId"
                      :url="`company/getAllList/forManager?${queryParams.customerId ? ('customerId=' + queryParams.customerId) : ''}`"
                      :objMap="{ value: 'id', label: 'title' }" placeholder="请选择" />
      </el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <page-table :url="url.list" ref="table" :tableForm="queryParams" :columns="columns">
      <template #action="scope">
        <el-tooltip content="修改" placement="top">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
        </el-tooltip>
      </template>
      <template #ruleD="{ row }">
        {{ `${row.ruleStartM}月${row.ruleStartD}日 - ${row.ruleEndM}月${row.ruleEndD}日` }}
      </template>
      <template #ruleH="{ row }">
        {{ `${row.ruleStartH}:${row.ruleStartMin} - ${row.ruleEndH}:${row.ruleEndMin}` }}
      </template>
      <template #status="scope">
        <el-switch v-model="scope.row.status" v-if="scope.row.status != undefined" :active-value="1" :inactive-value="0"
          @change="handleDelete(scope.row, scope.row.status)"></el-switch>
      </template>
    </page-table>

    <!-- 添加或修改价格配置对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="150px">

        <el-form-item label="企业" prop="companyId">
          <matic-select v-model:value="form.companyId"
                        :url="`company/getAllList/forManager`"
                        :objMap="{ value: 'id', label: 'title' }" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="电表" prop="devicesId">
          <matic-select v-model:value="form.devicesId" :url="`devices/getAllList/forManager?${form.companyId ? ('companyId=' + form.companyId) : ''}`"
                        :objMap="{ value: 'id', label: 'titleAndParent' }" placeholder="请选择" />
        </el-form-item>
        <el-form-item label="电价(度|kwh/元)" prop="priceYuan">
          <el-input v-model="form.priceYuan" placeholder="请输入" type="number" :min="0" />
        </el-form-item>
        <el-form-item label="电价月份" prop="yearMonth">
          <el-input v-model="form.yearMonth" placeholder="格式202401" type="number" :min="0" pattern="\d{6}"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Role">
import request from '@/utils/request'
const router = useRouter();
const { proxy } = getCurrentInstance();

const url = ref({
  list: 'devicesPrice/getPage/forManager',
  fine: 'devicesPrice/getOne/forManager',
  // delete: 'devicesPrice/disableOrUse/forManager',
  add: 'devicesPrice/save/forManager',
  edit: 'devicesPrice/save/forManager'
});

const open = ref(false);
const title = ref("");
const openDataScope = ref(false);
const table = ref(null);

const columns = ref([{
  key: 'company',
  title: '企业',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'devices',
  title: '电表',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'priceYuan',
  title: '电价(度|kwh/元)',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'yearMonth',
  title: '电价月份',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'createTime',
  title: '创建时间',
  ellipsis: true,
  tooltip: true,
},
{
  key: 'createBy',
  title: '创建人',
  ellipsis: true,
  tooltip: true,
},
{
  title: '操作',
  key: 'action',
  slots: {
    customKey: 'action',
  },
  align: 'center',
  fixed: 'right',
  width: 150
}
]);

const data = reactive({
  form: {},
  queryParams: {},
  rules: {
    companyId: [{ required: true, message: "企业不能为空", trigger: "blur" }],
    devicesId: [{ required: true, message: "电表不能为空", trigger: "blur" }],
    priceYuan: [{ required: true, message: "电价不能为空", trigger: "blur" }],
    yearMonth: [{ required: true, message: "电价月份类型为空", trigger: "blur" }],

  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  table.value.onload();
}
/** 搜索按钮操作 */
function handleQuery() {
  table.value.init();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    title: '',
    customerId: '',
  };
  proxy.resetForm("roleRef");
}
/** 添加价格 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加价格";
}
/** 修改价格 */
function handleUpdate(row) {
  reset();
  const id = row.id
  request.get(url.value.fine, { params: { id } }).then(response => {
    form.value = response.result;
    open.value = true;
    title.value = "修改价格";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["roleRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        request.post(url.value.edit, form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        request.post(url.value.add, form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}


</script>
<style scoped>
.guize {
  display: flex;
}

.guize span {
  padding: 0 5px;
}
</style>